
package ods_log

import (
	"fmt"
	"server/global"
	"time"
)


type OdsDbSyncLog struct {
	JobId     string    `json:"jobId" form:"jobId" gorm:"column:jobId;comment:任务ID;size:100;"`
	OdsTbName string    `json:"odsTbName" form:"odsTbName" gorm:"column:odsTbName;comment:目标表名;size:100;"`
	TimeBegin time.Time `json:"timeBegin" form:"timeBegin" gorm:"column:timeBegin;comment:开始时间;"`
	TimeEnd   time.Time `json:"timeEnd" form:"timeEnd" gorm:"column:timeEnd;comment:结束时间;"`
	TimeSpend float64   `json:"timeSpend" form:"timeSpend" gorm:"column:timeSpend;comment:耗时;"`
	JobType   string    `json:"jobType" form:"jobType" gorm:"column:jobType;comment:同步类型;"`
	SyncType  string    `json:"syncType" form:"syncType" gorm:"column:syncType;comment:同步方式;"`
	EndState  string    `json:"endState" form:"endState" gorm:"column:endState;comment:结果状态;size:20;"`
	EndReason string    `json:"endReason" form:"endReason" gorm:"column:endReason;comment:结果原因;size:255;"`
	Step      int       `json:"step" form:"step" gorm:"column:step;comment:同步步长;"`

	SrcTbCount int64 `json:"srcTbCount" form:"srcTbCount" gorm:"column:srcTbCount;comment:源表总行数;"`
	TgtTbCount int64 `json:"tgtTbCount" form:"tgtTbCount" gorm:"column:tgtTbCount;comment:目标表总行数;"`

	SrcScopeCount  int64 `json:"srcScopeCount" form:"srcScopeCount" gorm:"column:srcScopeCount;comment:源表范围同步行数;"`
	TgtScopeCount  int64 `json:"tgtScopeCount" form:"tgtScopeCount" gorm:"column:tgtScopeCount;comment:目标表范围同步行数;"`
	SyncScopeCount int64 `json:"syncScopeCount" form:"syncScopeCount" gorm:"column:syncScopeCount;comment:需同步行数;"`


	SyncTotal int64  `json:"syncTotal" form:"syncTotal" gorm:"column:syncTotal;comment:已同步总数;"`
	Success   int64  `json:"success" form:"success" gorm:"column:success;comment:成功总数;"`
	Fail      int64  `json:"fail" form:"fail" gorm:"column:fail;comment:失败总数;"`
	Remakes   string `json:"remakes" form:"remakes" gorm:"column:remakes;comment备注:;size:250;"`
	global.BaseModel
}


func (m *OdsDbSyncLog) TableName() string {
	return "ods_db_sync_log"
}


func (m *OdsDbSyncLog) Create() error {
	err := global.SysDB.Create(m).Error
	return err
}


func (m *OdsDbSyncLog) Save() error {
	err := global.SysDB.Save(m).Error
	return err
}

func (m *OdsDbSyncLog) SetErrLog(msg string) error {
	m.EndState = "失败"
	m.EndReason = msg
	m.TimeEnd = time.Now()
	m.TimeSpend = m.TimeEnd.Sub(m.TimeBegin).Seconds()
	fmt.Printf("数据同步失败,表%s,需同步%d条,成功%d条,失败%d条,失败原因:%s。\n", m.OdsTbName, m.SyncTotal, m.Success, m.Fail, m.EndReason)
	if err := m.Save(); err != nil {
		return fmt.Errorf("更新数据日志失败:%v", err)
	}
	return nil
}

func (m *OdsDbSyncLog) SetSuccessLog(msg string) error {
	m.EndState = "成功"
	m.EndReason = msg
	m.TimeEnd = time.Now()
	m.TimeSpend = m.TimeEnd.Sub(m.TimeBegin).Seconds()
	ev := int64(float64(m.SyncTotal) / m.TimeSpend)
	fmt.Printf("数据同步成功:表%s,%s,%s,需同步%d条,成功%d条,失败%d条,耗时%f秒,平均%d条/秒。\n", m.OdsTbName, m.JobType, m.SyncType, m.SyncTotal, m.Success, m.Fail, m.TimeSpend, ev)
	if err := m.Save(); err != nil {
		return fmt.Errorf("更新数据日志失败:%v", err)
	}
	return nil
}
